package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.util.CrowdUtil;
import com.github.pagehelper.PageInfo;
import entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AdminHandler {
    @Autowired
    private AdminService adminService;
//    执行用户的更新操作
    @RequestMapping("admin/updata.html")
    public String updateAdmin(Admin admin,@RequestParam("pageNum") Integer pageNum,
                              @RequestParam("keyword") String keyword){
        adminService.updateAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum="+pageNum + "&keyword="+keyword;
    }
//用户在page页面点击编辑小按钮
    @RequestMapping("admin/to/edit/page.html")
    public String toEditPage(@RequestParam("admin") Integer adminId,
                             ModelMap modelMap){
        Admin admin=adminService.getAdminById(adminId);
        modelMap.addAttribute("admin",admin);
        return "admin-edit";
    }
    //    添加
    @PreAuthorize("hasAuthority('user:save')")
    @RequestMapping("admin/save.html")
    public String save(Admin admin) {
      adminService.saveAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum="+Integer.MAX_VALUE;
    }
//删除
    @RequestMapping("/admin/page/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(
            @PathVariable("adminId") Integer adminId,
            // 从前端获取的当前页码与关键字（为了删除后跳转的页面仍然是刚才的页面，优化体验）
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("keyword") String keyword) {
        adminService.remove(adminId);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    //翻页
    @RequestMapping("/admin/get/page.html")
    public String getInfo(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                          ModelMap modelMap) {
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
        modelMap.addAttribute(CrowdConstant.NAME_PAGE_INFO, pageInfo);
        return "admin-page";
    }

    @RequestMapping("/admin/do/logout.html")
    public String dpLogout(HttpSession session) {
//        强制session失效导致登陆失败
        session.invalidate();
        return "redirect:/admin/login/page.html";
    }


    @RequestMapping("/admin/do/login.html")
    public String doLogin(@RequestParam("LoginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,
                          HttpSession session) {
// 调用 Service 方法执行登录检查
// 这个方法如果能够返回 admin 对象说明登录成功，如果账号、密码不正确则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);
        System.out.println(admin);
        // 将登录成功返回的 admin 对象存入 Session 域
        session.setAttribute(CrowdConstant.LOGIN_ADMIN_NAME, admin);
        return "redirect:/admin/main/page.html";
    }
}
